package com.ocom.app.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.app.dao.IDaoTUser;
import com.ocom.app.service.TLoginAccountService;
import com.ocom.common.entity.app.EntityTLoginAccount;
import com.ocom.common.entity.app.EntityTUser;
import com.ocom.app.service.TUserService;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.app.FindOrRegVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TUserServiceImpl extends ServiceImpl<IDaoTUser, EntityTUser> implements TUserService {


  @Autowired
  private IDaoTUser iDaoTUser;

  @Autowired
  private TLoginAccountService tLoginAccountService;

  @Override
  @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
  public FindOrRegVo findOrRegByPhoneNum(String phoneNum) {
    return iDaoTUser.findOrRegByPhoneNum(phoneNum);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
  public Boolean destroy(Long userId) {
    tLoginAccountService.remove(Wrappers.lambdaQuery(EntityTLoginAccount.class).eq(EntityTLoginAccount::getUserId,userId));
    this.remove(Wrappers.lambdaQuery(EntityTUser.class).eq(EntityTUser::getId,userId));
    return true;
  }
}
